package com.ggh.jinjilive.ui.mall.shop.adapter;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.bumptech.glide.Glide;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.ItemAttentionShopBinding;
import com.ggh.jinjilive.databinding.ItemMallGoodsBinding;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.listener.AddOnClickListener;
import com.ggh.jinjilive.mobile.mall.MallGoods;
import com.ggh.jinjilive.mobile.mall.shop.AttentionShopMobile;

import java.util.List;

public class AttentionShopAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<AttentionShopMobile> attentionShopMobileList;

    public void setAdd(List<AttentionShopMobile> attentionShopMobileList) {
        this.attentionShopMobileList = attentionShopMobileList;
        notifyDataSetChanged();
    }

    public void ADD(List<AttentionShopMobile> attentionShopMobileList) {
        if(null !=  this.attentionShopMobileList){
            this.attentionShopMobileList.addAll(attentionShopMobileList);
        }else {
            this.attentionShopMobileList = attentionShopMobileList;
        }
        notifyDataSetChanged();
    }


    private String management;
    public void setManagement(String management){
        this.management = management;
        notifyDataSetChanged();
    }




    private AddItemOnClickListener addItemOnClickListener;
    public void setAddItemOnClickListener(AddItemOnClickListener addItemOnClickListener) {
        this.addItemOnClickListener = addItemOnClickListener;
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemAttentionShopBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_attention_shop, parent, false);
        return new ItemViewHolder(binding);
    }

    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder recyclerViewholder,final int position) {
       if(recyclerViewholder instanceof ItemViewHolder){
            ItemViewHolder holder = (ItemViewHolder) recyclerViewholder;
           AttentionShopMobile attentionShopMobile = attentionShopMobileList.get(position);
            if (null != attentionShopMobile) {
                //图片
                Glide.with(holder.binding.storeImg).load(attentionShopMobile.getImg()).into(holder.binding.storeImg);
                //名称
                holder.binding.shopName.setText(attentionShopMobile.getName());
                //关注人数
                holder.binding.tvAttention.setText(attentionShopMobile.getCollect_num() +"人关注");
                //管理是否显示CheckBox
                if(null != this.management && this.management.equals("管理")){
                    holder.binding.cbCheckall.setVisibility(View.VISIBLE);
                }else {
                    holder.binding.cbCheckall.setVisibility(View.GONE);
                }
                holder.binding.llStore.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if(null != addItemOnClickListener){
                            addItemOnClickListener.onSkipClicked(position,attentionShopMobile);
                        }
                    }
                });
                //CheckBox设置事件监听
                holder.binding.cbCheckall.setChecked(attentionShopMobile.isSelected());
                holder.binding.cbCheckall.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if(holder.binding.cbCheckall.isChecked()){
                            attentionShopMobileList.get(position).setSelected(true);
                        }else {
                            attentionShopMobileList.get(position).setSelected(false);
                        }
                        notifyItemChanged(position);
                        if(null != addItemOnClickListener){
                            addItemOnClickListener.onSkipClicked(999999,attentionShopMobileList);
                        }
                    }
                });
            }
        }

    }

    @Override
    public int getItemCount() {
        return null == attentionShopMobileList ? 0 : attentionShopMobileList.size();
    }


    class ItemViewHolder extends RecyclerView.ViewHolder {
        ItemAttentionShopBinding binding;
        public ItemViewHolder(@NonNull ItemAttentionShopBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

}